/**
 * 
 */
package com.xbang.attendance.web.action.warehouse.exchange;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.xbang.attendance.entity.product.Product;
import com.xbang.attendance.entity.stock.Inventory;
import com.xbang.attendance.entity.stock.InventoryExchange;
import com.xbang.attendance.entity.system.Warehouse;
import com.xbang.attendance.service.InventoryExchangeService;
import com.xbang.attendance.service.InventoryService;
import com.xbang.attendance.service.MailService;
import com.demo2do.lighturl.annotation.Action;
import com.xbang.core.struts.support.BaseActionSupport;

/**
 * @author dev.zhang
 * 
 */
public class ExchangeManager extends BaseActionSupport {

	private static final long serialVersionUID = 5564954399670936978L;

	private final static Log logger = LogFactory.getLog(ExchangeManager.class);

	@Autowired
	private InventoryService inventoryService;
	@Autowired
	private MailService mailService;
	
	@Autowired
	private InventoryExchangeService inventoryExchangeService;

	private List<Warehouse> chooseWarehouse = new ArrayList<Warehouse>();

	private List<Product> chooseProduct = new ArrayList<Product>();

	private InventoryExchange inventoryExchange;

	private Integer id;
	
	

	@Action("/warehouse/exchange/add")
	public String add() throws Exception {
		getMoreInfo();
		return "edit";
	}
	
	@Action("/warehouse/exchange/${id}/mail")
	public String sendMail()throws Exception{
		if(id != null){
			Inventory inventory =(Inventory) inventoryService.get(Inventory.class, id);
			List list=	inventoryExchangeService.getInventoryExchangesByInventory(inventory );
			mailService.sendMail(list, inventory.getWarehouse());
		}
		
		return "r:/warehouse/exchange/"+id+"/inventory";
	}

	@Action("/warehouse/exchange/${id}/export")
	public String export() throws Exception {
		String webPath =ServletActionContext.getRequest().getRealPath("//download//order//purchase");
		HttpServletRequest request = ServletActionContext.getRequest();
		String urlPath = request.getRequestURL().toString();
		String contextPath = request.getContextPath();
		String contextURL = urlPath.split(contextPath)[0] + contextPath;
		String exportFile = contextURL + "/download/order/purchase/" + inventoryExchangeService.genericExcelFile(webPath,id);
		return "r:" + exportFile;
	}
	
	
//	private Result getMyResult(Date date)throws Exception{
//		if (id != null) {
//			
//			if (inventory != null) {
//				
//				return inventoryService.searchByPage(hqlSentence, searchComponent, page);
//			}
//		}
//		return null;
//	}
	@Action("/warehouse/exchange/${id}/edit")
	public String edit() throws Exception {
		getMoreInfo();
		if (inventoryExchange == null && id != null) {
			inventoryExchange = inventoryService.getInventoryExchangeByKey(id);
		}
		return "edit";
	}

	@Action("/warehouse/exchange/${id}/view")
	public String view() throws Exception {

		if (id != null) {
			inventoryExchange = inventoryService.getInventoryExchangeByKey(id);
		}

		return "view";
	}

	@Action("/warehouse/exchange/${id}/delete")
	public String delete() throws Exception {

		if (id != null) {
			inventoryService.delete(InventoryExchange.class, id);
		}

		return "r:/warehouse/exchange/list";
	}

	@Action("/warehouse/exchange/save")
	public String save() throws Exception {

//		if (inventoryExchange != null) {
//			inventoryService.inventoryExchange(inventoryExchange.getSourceWarehouse(), inventoryExchange.getIndentWarehouse(), inventoryExchange.getProduct(),
//					inventoryExchange.getAmount(), null, inventoryExchange.getInvertoryOperation());
//			
//		}

		return "r:/warehouse/exchange/list";
	}

	private void getMoreInfo() throws Exception {
		chooseProduct = inventoryService.getObjects(Product.class);
		chooseWarehouse = inventoryService.getObjects(Warehouse.class);

	}

	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * @return the chooseWarehouse
	 */
	public List<Warehouse> getChooseWarehouse() {
		return chooseWarehouse;
	}

	/**
	 * @param chooseWarehouse
	 *            the chooseWarehouse to set
	 */
	public void setChooseWarehouse(List<Warehouse> chooseWarehouse) {
		this.chooseWarehouse = chooseWarehouse;
	}

	/**
	 * @return the chooseProduct
	 */
	public List<Product> getChooseProduct() {
		return chooseProduct;
	}

	/**
	 * @param chooseProduct
	 *            the chooseProduct to set
	 */
	public void setChooseProduct(List<Product> chooseProduct) {
		this.chooseProduct = chooseProduct;
	}

	/**
	 * @return the inventoryExchange
	 */
	public InventoryExchange getInventoryExchange() {
		return inventoryExchange;
	}

	/**
	 * @param inventoryExchange
	 *            the inventoryExchange to set
	 */
	public void setInventoryExchange(InventoryExchange inventoryExchange) {
		this.inventoryExchange = inventoryExchange;
	}

	/**
	 * @param inventoryService
	 *            the inventoryService to set
	 */
	public void setInventoryService(InventoryService inventoryService) {
		this.inventoryService = inventoryService;
	}

}
